<template>
  <a-modal
    :title="title"
    :width="800"
    :ok="false"
    :visible="visible"
    :confirm-loading="confirmLoading"
    :ok-button-props="{ props: {disabled: disableSubmit} }"
    cancel-text="关闭"
    @ok="handleOk"
    @cancel="handleCancel"
  >

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="机构名称"
          :hidden="false"
          has-feedback
        >
          <a-input id="departName" v-decorator="['departName', validatorRules.departName ]" placeholder="请输入机构/部门名称" />
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" :hidden="seen" label="上级部门" has-feedback>
          <a-tree-select
            v-model="model.parentId"
            style="width:100%"
            :dropdown-style="{maxHeight:'200px',overflow:'auto'}"
            :tree-data="departTree"
            placeholder="请选择上级部门"
            :disabled="condition"
          />
        </a-form-item>
        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="分组名"
        >
          <a-input v-decorator="['groupName', {'initialValue':''}]" placeholder="请输入分组名" />
        </a-form-item>
        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="电话"
        >
          <a-input v-decorator="['mobile',validatorRules.mobile]" placeholder="请输入电话" />
        </a-form-item>
        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="传真"
        >
          <a-input v-decorator="['fax', {}]" placeholder="请输入传真" />
        </a-form-item>
        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="地址"
        >
          <a-input v-decorator="['address', {}]" placeholder="请输入地址" />
        </a-form-item>
        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="排序"
        >
          <a-input-number v-decorator="[ 'departOrder',{'initialValue':0}]" />
        </a-form-item>
        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="备注"
        >
          <a-textarea v-decorator="['memo', {}]" placeholder="请输入备注" />
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
// import { queryIdTree } from '@/api/api'
import pick from 'lodash.pick'
export default {
  name: 'SysDepartModal',
  data () {
    return {
      departTree: [],
      orgTypeData: [],
      phoneWarning: '',
      departName: '',
      title: '操作',
      seen: false,
      visible: false,
      condition: true,
      disableSubmit: false,
      model: {},
      menuhidden: false,
      menuusing: true,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        departName: { rules: [{ required: true, message: '请输入机构/部门名称!' }] },
        orgCode: { rules: [{ required: true, message: '请输入机构编码!' }] },
        mobile: { rules: [{ validator: this.validateMobile }] }
      },
      url: {
        add: '/sysdepart/sysDepart/add'
      }
    }
  },
  created () {
  },
  methods: {
    loadTreeData () {
      var that = this
      this.$authApi.queryIdTree().then((res) => {
        if (res.code === '000000') {
          console.log(res)
          that.departTree = []
          for (let i = 0; i < res.data.length; i++) {
            const temp = res.data[i]
            that.departTree.push(temp)
          }
        }
      })
    },
    add (depart) {
      if (depart) {
        this.seen = false
      } else {
        this.seen = true
      }
      this.edit(depart)
    },
    edit (record) {
      this.form.resetFields()
      this.model = Object.assign({}, {})
      this.visible = true
      this.loadTreeData()
      this.model.parentId = record != null ? record.toString() : null
      this.$nextTick(() => {
        this.form.setFieldsValue({ mobile: '', addrss: '', memo: '' })
        console.log(pick(this.model, 'departName', 'departNameEn', 'departNameAbbr', 'departOrder', 'description', 'orgType', 'orgCode', 'mobile', 'fax', 'address', 'memo', 'status', 'delFlag'))
        // this.form.setFieldsValue(pick(this.model, 'departName', 'departNameEn', 'departNameAbbr', 'departOrder', 'description', 'orgType', 'orgCode', 'mobile', 'fax', 'address', 'memo', 'status', 'delFlag'))
      })
    },
    close () {
      this.$emit('close')
      this.disableSubmit = false
      this.visible = false
    },
    handleOk () {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          const formData = Object.assign(this.model, values)
          // 时间格式化
          console.log(formData)
          this.$authApi.addDept(formData).then((res) => {
            if (res.code === '000000') {
              that.$message.success(res.mesg)
              that.loadTreeData()
              that.$emit('ok')
            } else {
              that.$message.warning(res.data || res.mesg)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        }
      })
    },
    handleCancel () {
      this.close()
    },
    validateMobile (rule, value, callback) {
      if (!value || new RegExp(/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(value)) {
        callback()
      } else {
        callback(new Error('您的手机号码格式不正确!'))
      }
    }
  }
}
</script>

<style scoped>

</style>
